package com.tnxst.phexamination.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.adapter.ImageAdapter;
import com.tnxst.phexamination.adapter.PackageServiceAdapter;
import com.tnxst.phexamination.constant.HISConfig;
import com.tnxst.phexamination.databinding.ActivityServicePackageBinding;
import com.tnxst.phexamination.dto.ServicePackageDTO;
import com.tnxst.phexamination.public_health_docking_interface.FamilySignInterface;
import com.tnxst.phexamination.utils.ReflectUtils;
import com.tnxst.phexamination.utils.StringUtils;
import com.tnxst.phexamination.utils.Utils;
import com.tnxst.phexamination.utils.WebServiceUtils;
import com.tnxst.phexamination.utils.WebUtils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PackageServiceActivity extends BaseActivity{

    private GlobalViewModel viewModel;

    private ActivityServicePackageBinding binding;

    private List<Object> path;

    private List<ServicePackageDTO> dtos;

    private PackageServiceAdapter packageServiceAdapter;

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this)).get(GlobalViewModel.class);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_service_package);
        binding.setServicePackage(viewModel);
        binding.setLifecycleOwner(this);
        initData();
        initView();
        initBannerView();
        useBanner();
    }

    private void initData() {
        context = this;
        dtos = viewModel.getServicePackageRepository().findAll();
        packageServiceAdapter = new PackageServiceAdapter(dtos,getSupportFragmentManager());
        binding.recycler.setAdapter(packageServiceAdapter);
        binding.recycler.setLayoutManager(new LinearLayoutManager(context));
        packageServiceAdapter.notifyDataSetChanged();
//        if (StringUtils.isNotNull(HISConfig.PRODUCT_CODE)){
//            FamilySignInterface.getOrgServicePack(null, 10, new WebServiceUtils.Response<List>() {
//                @Override
//                public void onSuccess(List result) {
//                    ServicePackageDTO[] servicePackageDTOS = new ServicePackageDTO[result.size()];
//                    for (int i = 0; i < result.size(); i++) {
//                        Map map = (Map) result.get(i);
//                        ServicePackageDTO packageDTO = (ServicePackageDTO) ReflectUtils.mapToObject(map, ServicePackageDTO.class);
//                        servicePackageDTOS[i] = packageDTO;
//                    }
//                    viewModel.getServicePackageRepository().insert(servicePackageDTOS);
//                    dtos = viewModel.getServicePackageRepository().findAll();
//                    packageServiceAdapter.setSignPackages(dtos);
//                    packageServiceAdapter.notifyDataSetChanged();
//                }
//                @Override
//                public void onError(String error) {
//                    Utils.Toast(PackageServiceActivity.this,error);
//                }
//            });
//            FamilySignInterface.getOrgServicePack(null, 11, new WebServiceUtils.Response<List>() {
//                @Override
//                public void onSuccess(List result) {
//                    ServicePackageDTO[] servicePackageDTOS = new ServicePackageDTO[result.size()];
//                    for (int i = 0; i < result.size(); i++) {
//                        Map map = (Map) result.get(i);
//                        ServicePackageDTO packageDTO = (ServicePackageDTO) ReflectUtils.mapToObject(map, ServicePackageDTO.class);
//                        servicePackageDTOS[i] = packageDTO;
//                    }
//                    viewModel.getServicePackageRepository().insert(servicePackageDTOS);
//                    dtos = viewModel.getServicePackageRepository().findAll();
//                    packageServiceAdapter.setSignPackages(dtos);
//                    packageServiceAdapter.notifyDataSetChanged();
//                }
//                @Override
//                public void onError(String error) {
//                    Utils.Toast(PackageServiceActivity.this,error);
//                }
//            });
//        }
    }

    private void initView() {
        binding.editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString();
                if (StringUtils.isNotNull(text)){
                    List<ServicePackageDTO> like = viewModel.getServicePackageRepository().findByLike("SERVICENAME", text);
                    dtos = like;
                    packageServiceAdapter.setSignPackages(dtos);
                    //适配器刷新
                    packageServiceAdapter.notifyDataSetChanged();
                }else {
                    List<ServicePackageDTO> all = viewModel.getServicePackageRepository().findAll();
                    dtos = all;
                    packageServiceAdapter.setSignPackages(dtos);
                    //适配器刷新
                    packageServiceAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void initBannerView() {
        path = new ArrayList<>();
        path.add(R.drawable.home_banner_one);
        path.add(R.drawable.home_banner_two);
        path.add(R.drawable.home_banner_three);
        path.add(R.drawable.home_banner_four);
    }

    private void useBanner() {
        //--------------------------简单使用-------------------------------
        binding.banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(new ImageAdapter(path, this))
                .setIndicator(new CircleIndicator(this))
                .start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding.banner.destroy();
    }

}
